﻿using System;
using System.Collections.Generic;
using System.Data;
using System.DataProvider;
using System.Linq;
using System.Text;
using System.Utility.Data;
using System.Utility.DBMS;
using System.Utility.Extensions.Attributes;

namespace Domain
{
    public enum AppUserType
    {
        Admin, DealershipStaff, AppUser
    }

    public class AppUser
    {

        public string Name { get; set; }
        public string Password { get; set; }
        public string EmailId { get; set; }
        public string Description { get; set; }

        [Include(false)]
        public AppUserType Type { get; set; }

        public static AppUser GetUser(string userName, string password)
        {
            AppUser user = null;
            IDataProvider provider = new IndexBasedDataProvider();
            try
            {
                //password = PasswordProtector.Encrypt(password);
                DataSet set = provider.GetDataSet("ValidateAndLoadUserInfo", new object[] { userName, password });
                DataTable table = set.Tables[0];
                user = new AppUser();
                DBHelper.PopulateObject(user, table.Rows[0]);
                user.Type = AppUserType.AppUser;
            }
            catch (Exception ex)
            {
                user = null;
            }
            return user;
        }

        public static AppUser AddUser(string userName, string emailid, string password)
        {
            AppUser user = null;
            IDataProvider provider = new IndexBasedDataProvider();
            password = PasswordProtector.Encrypt(password);
            DataSet set = provider.GetDataSet("AddUser", new object[] { userName, emailid, password });
            DataTable table = set.Tables[0];
            user = new AppUser();
            DBHelper.PopulateObject(user, table.Rows[0]);
            user.Type = AppUserType.Admin;
            return user;
        }
    }
}
